package com.guigu.qingcheng.systemservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.system.TbMenu;
import com.guigu.qingcheng.systemservice.service.TbMenuService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/systemservice/tb-menu")
public class TbMenuController {
    @Autowired
    private TbMenuService service;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/menu/findAll")
    public R findAll(){
        List<TbMenu> list = service.list(null);
        if(list.size() <= 0)
            return R.error().data("items", list);
        return R.ok().data("items", list);
    }

    /**
     * 查询菜单
     * @return
     */
    @GetMapping("/findMenu")
    public List<Map> findMenu(){
        return service.findAllMenu();
    }

    /**
     * 查询菜单
     * @return
     */
    @GetMapping("/findMenu2")
    public List<Map> findMenu2(@RequestParam("admin") String admin){
        return service.findAllMenu2(admin);
    }

    /**
     * 分页查询数据
     * @param page 页号
     * @param size 页大小
     * @return
     */
    @GetMapping("/menu/findPage")
    public R findPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<TbMenu> pg = new Page<>(page, size);
        IPage<TbMenu> list = service.page(pg, null);
        if(pg.getRecords().size() <= 0)
            return R.error().data("total", pg.getTotal()).data("pgitems", pg.getRecords());
        return R.ok().data("total", pg.getTotal()).data("pgitems", pg.getRecords());
    }

    /**
     * 条件查询数据
     * @param searchMap, 查询所带条件
     * @return
     */
    @PostMapping("/menu/findList")
    public R findList(@RequestBody Map<String, Object> searchMap){
        List<TbMenu> list = service.findList(searchMap);
        if(list.size() <= 0)
            return R.error().data("listItems", list);
        return R.ok().data("listItems", list);
    }

    /**
     * 分页+条件查询
     * @param page 页号
     * @param size 页大小
     * @param searchMap 条件
     * @return
     */
    @PostMapping("/menu/findPage")
    public R findPage(@RequestBody(required=false) Map<String, Object> searchMap, Integer page, Integer size){
        Page<TbMenu> pg = service.findPage(page, size, searchMap);
        if(pg.getRecords().size() <= 0)
            return R.error().data("total", pg.getTotal()).data("fpgitems", pg.getRecords());
        return R.ok().data("total", pg.getTotal()).data("fpgitems", pg.getRecords());
    }

    /**
     * 根据id查询数据
     * @param id 所传id
     * @return
     */
    @GetMapping("/menu/findById")
    public R findById(@RequestParam("id") Integer id){
        TbMenu tbMenu = service.getById(id);
        if(tbMenu == null)
            return R.error().data("pojo", tbMenu);
        return R.ok().data("pojo", tbMenu);
    }

    /**
     * 增加
     * @param tbMenu 增加的信息
     * @return
     */
    @PostMapping("/menu/add")
    public R add(@RequestBody TbMenu tbMenu){
        Boolean f = service.save(tbMenu);
        if(f == false)
            return R.error();
        return R.ok();
    }

    /**
     * 修改
     * @param tbMenu
     * @return
     */
    @PostMapping("/menu/update")
    public R update(@RequestBody TbMenu tbMenu){
        Boolean f = service.updateById(tbMenu);
        if(f == false)
            return R.error();
        return R.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/menu/delete")
    public R delete(@RequestParam("id") String id){
        Boolean f = service.removeById(id);
        if(f == false)
            return R.error();
        return R.ok();
    }
}

